package list.control;

import java.util.ArrayList;

import static java.util.Collections.sort;

/**
 * Created by ZhipengLi.
 */
public class Searcher {
    static class Partner<T> implements Comparable {
        private Object main;

        private double percent;

        public Partner() {
        }

        public Object getMain() {
            return main;
        }

        public double getPercent() {
            return percent;
        }

        public void setMain(T main) {
            this.main = main;
        }

        public void setPercent(double percent) {
            this.percent = percent;
        }

        @Override
        public int compareTo(Object o) {
            Partner other = (Partner) o;
            return (int) ((other.getPercent() - this.percent) * 10000);
        }
    }

    public static <T> T[] simpleMatch(T target, T[] pattern) {
        ArrayList<Partner> partners = new ArrayList<>();
        String tar = target.toString();
        for (T aPattern : pattern) {
            if (null == aPattern) continue;
            double percent = simpleMatch(tar, aPattern.toString(), 1);
            Partner<T> partner = new Partner<>();
            partner.setPercent(percent);
            partner.setMain(aPattern);
            partners.add(partner);
        }
        sort(partners);
        for (int i = 0; i < pattern.length; i++) {
            if (partners.get(i).getPercent() > 0.05) {
                pattern[i] = (T) partners.get(i).getMain();
            } else {
                pattern[i] = null;
            }
        }
        return pattern;
    }

    //简易匹配，返回匹配程度
    private static double simpleMatch(String target, String pattern, int times) {
        int lenTar = target.length();
        int lenPat = pattern.length();
        if (1 < lenTar) {
            if (lenPat > lenTar) {
                if (pattern.contains(target)) {
                    return 1;
                }
                double percent = 0;//匹配度
                int length = lenTar - 1;//下次匹配的长度
                for (int i = 0; i < times && i + length < lenTar; i++) {
                    percent += simpleMatch(target.substring(i, i + length - 1), pattern, times + 1);
                }
                return percent * ((double) lenTar / (double) lenPat);
            } else {
                if (target.contains(pattern)) {
                    return (double) lenPat / (double) lenTar;
                }
            }
        } else {
            if (pattern.contains(target)) {
                return (double) lenTar / (double) lenPat / (double) times;
            }
        }
        return 0;
    }

    /*// 根据Unicode编码完美的判断中文汉字和符号
    private static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        return ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION;
    }

    // 完整的判断中文汉字和符号
    public static boolean hasChinese(String strName) {
        char[] ch = strName.toCharArray();
        for (char c : ch) {
            if (isChinese(c)) {
                return true;
            }
        }
        return false;
    }*/

}
